package com.kaili.basic.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.DriveLoginLogQueryDto;
import com.kaili.basic.pojo.dto.SysLogQueryDto;
import com.kaili.basic.pojo.dto.SysLoginLogQueryDto;
import com.kaili.basic.pojo.entity.DriveLoginLogEntity;
import com.kaili.basic.pojo.entity.SysLoginLogEntity;
import com.kaili.basic.pojo.vo.SysLogExportVO;
import com.kaili.basic.service.BaseLogService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.pojo.entity.LoginDetail;
import com.kaili.common.pojo.entity.SysLogEntity;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.CommonUtil;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "日志管理", description = "日志管理相关API")
public class BaseLogController {
    @Autowired
    private BaseLogService baseLogService;

    @GetMapping("/logs/sys")
    @ApiOperation(value = "分页查询-系统日志", notes = "分页查询-系统日志事件")
    public Page<SysLogEntity> querySysLog(@ModelAttribute SysLogQueryDto dto, @ApiParam(hidden = true)HttpServletRequest request) {
        return baseLogService.querySysLog(CommonUtil.getMongodbName(request,"sys_log"),dto);
    }

    @GetMapping("/logs/sys/export")
    @ApiOperation(value = "导出-系统日志", notes = "导出-系统日志事件")
    public void querySysLogExport(@ModelAttribute SysLogQueryDto dto,
                                                  @ApiParam(hidden = true)HttpServletResponse response,
                                                  @ApiParam(hidden = true)HttpServletRequest request) throws RestException {
        List<SysLogExportVO> list = baseLogService.querySysLogExport(CommonUtil.getMongodbName(request, "sys_log"), dto);
        ExcelUtil.exportExcel(list,"系统日志","sheet1", SysLogExportVO.class, UUID.randomUUID().toString()+".xls",response);
    }

    @GetMapping("/logs/sysLogin")
    @ApiOperation(value = "分页查询-系统登录日志", notes = "分页查询-系统登录日志事件")
    public Page<SysLoginLogEntity> querySysLoginLog(@ModelAttribute SysLoginLogQueryDto dto,@ApiParam(hidden = true)HttpServletRequest request) {
        String collectionName = CommonUtil.getMongodbName(request,"sys_login_log");
        return baseLogService.querySysLoginLog(dto,collectionName);
    }

    @GetMapping("/logs/sysLogin/export")
    @ApiOperation(value = "导出-系统登录日志", notes = "导出-系统登录日志事件")
    public void querySysLoginLogExport(@ModelAttribute SysLoginLogQueryDto dto,
                                  @ApiParam(hidden = true)HttpServletResponse response,
                                  @ApiParam(hidden = true)HttpServletRequest request) throws RestException {
        List<SysLoginLogEntity> list = baseLogService.querySysLogExport(dto,CommonUtil.getMongodbName(request, "sys_login_log"));
        ExcelUtil.exportExcel(list,"系统登录日志","sheet1", SysLoginLogEntity.class, UUID.randomUUID().toString()+".xls",response);
    }

    @PostMapping("/logs/sysLogout")
    @ApiOperation(value = "保存系统-退出日志", notes = "保存系统-退出日志事件")
    public void queryLogOne(@ApiParam(hidden = true)HttpServletRequest request) {
        //保存退出日志
        String collectionName = CommonUtil.getMongodbName(request,"sys_login_log");
        SysLoginLogEntity logOne = baseLogService.queryLogOne(LoginDetailUtils.getCurrent().getUsername(), collectionName);
        logOne.setLogoutTime(new Date());
        baseLogService.saveSysLoginLog(logOne,collectionName);
    }

    @PostMapping("/logs/drive")
    @ApiOperation(value = "新增设备注册日志", notes = "新增设备注册日志事件")
    public RestStatus saveDriveLogs(@RequestBody DriveLoginLogEntity entity, @ApiParam(hidden = true)HttpServletRequest request) {
         baseLogService.saveDriveLogs(entity,CommonUtil.getMongodbName(request,"drive_login_log"));
         return CommonStatus.ADD_OK;
    }

    @GetMapping("/logs/drive")
    @ApiOperation(value = "分页查询-设备日志", notes = "分页查询-设备日志事件")
    public Page<DriveLoginLogEntity> queryDriveLogs(@ModelAttribute DriveLoginLogQueryDto queryDto,
                                                    @ApiParam(hidden = true)HttpServletRequest request) {
        return baseLogService.queryDriveLogs(CommonUtil.getMongodbName(request,"drive_login_log"),queryDto);
    }

    @GetMapping("/logs/drive/export")
    @ApiOperation(value = "导出-设备日志", notes = "分页查询-设备日志事件")
    public void queryDriveLogExport(@ModelAttribute DriveLoginLogQueryDto dto,
                                       @ApiParam(hidden = true)HttpServletResponse response,
                                       @ApiParam(hidden = true)HttpServletRequest request) throws RestException {
        List<DriveLoginLogEntity> list = baseLogService.queryDriveLogExport(dto,CommonUtil.getMongodbName(request, "drive_login_log"));
        ExcelUtil.exportExcel(list,"设备登录日志","sheet1", DriveLoginLogEntity.class, UUID.randomUUID().toString()+".xls",response);
    }
}
